package com.csun.dsms.controller;

import com.csun.dsms.comment.base.enums.ErrorCodeEnum;
import com.csun.dsms.comment.base.exception.BusinessException;
import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.enums.DisabilityDetailsEnum;
import com.csun.dsms.enums.DisabilityTypeEnum;
import com.csun.dsms.model.domain.DsmsUnit;
import com.csun.dsms.model.domain.YcAlert;
import com.csun.dsms.model.domain.YcDevice;
import com.csun.dsms.model.domain.YcDeviceData;
import com.csun.dsms.model.dto.poor.PoorDto;
import com.csun.dsms.model.dto.yc.YcDataDto;
import com.csun.dsms.model.dto.yc.YcDataJinDto;
import com.csun.dsms.model.dto.yc.YcDeviceQueryDto;
import com.csun.dsms.model.huang.HgGdpData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.model.huang.HgYearMonthVo;
import com.csun.dsms.service.HgGdpDataService;
import com.csun.dsms.service.YcAlertService;
import com.csun.dsms.service.YcDeviceDataService;
import com.csun.dsms.service.YcDeviceService;
import com.csun.dsms.utils.DateUtil;
import com.csun.dsms.utils.PublicUtil;
import com.csun.dsms.utils.ReadExcelUtils;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hgGdp")
public class HgGdpController {

    @Resource
    private HgGdpDataService hgGdpDataServiceImpl;

    /*
    * 设备的添加
    * */
    @Authorization
    @RequestMapping(value = "/dataAdd",method = RequestMethod.POST)
    public Wrapper gdpDataAdd (@RequestBody HgGdpData hgGdpData){
        if(PublicUtil.isEmpty(hgGdpData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgGdpDataServiceImpl.save(hgGdpData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }


    /*
     * 分页查询
     * */
    @Authorization
    @RequestMapping(value = "/gdpDataQuery",method = RequestMethod.POST)
    public Wrapper deviceQuery (@RequestBody HgQueryDto hgQueryDto){
        List<HgGdpData> query = hgGdpDataServiceImpl.queryGdpDataList(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    @Authorization
    @RequestMapping(value = "/dataUpdate",method = RequestMethod.POST)
    public Wrapper gdpDataUpdate (@RequestBody HgGdpData hgGdpData){
        if(PublicUtil.isEmpty(hgGdpData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgGdpDataServiceImpl.edit(hgGdpData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    @Authorization
    @RequestMapping(value = "/groupByMonth",method = RequestMethod.POST)
    public Wrapper groupByMonth (@RequestBody HgQueryDto hgQueryDto){
        List<HgYearMonthVo> query = hgGdpDataServiceImpl.groupByMonth(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }


    @Transactional
    @PostMapping(consumes = "multipart/form-data", value = "/toLead")
    public Wrapper importPoor(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        ReadExcelUtils excelReader = new ReadExcelUtils(file.getInputStream());
        Map<Integer, Map<Integer, Object>> map = excelReader.readExcelContent();
        for (int i = 2; i <= map.size(); i++) {
//            String yearStr = "统计年度";
//            String qStr = "统计季度";
//            String areaName = "区划地名";
//            String industry = "行业类型(可不填)";
//            String gdp = "地区生产总值";
//            String ranking = "地区生产总值排名";
//            String growthGdp = "GDP增加值";
//            String growthRate = "GDP增速(不用百分号)";
//            String topRate = "上级GDP增速(不用百分号)";
//            String growthRateRanking = "增加值增速排名";
//            String creator = "填报人";
//            String entryDepartment = "填报部门";
            String yearStr = (String) map.get(i).get(0);
            String qStr = (String) map.get(i).get(1);
            String areaName = (String) map.get(i).get(2);
            String industry = (String) map.get(i).get(3);
            String gdp = (String) map.get(i).get(4);
            String ranking = (String) map.get(i).get(5);
            String growthGdp = (String) map.get(i).get(6);
            String growthRate = (String) map.get(i).get(7);
            String topRate = (String) map.get(i).get(8);
            String growthRateRanking = (String) map.get(i).get(9);
            String creator = (String) map.get(i).get(10);
            String entryDepartment = (String) map.get(i).get(11);

            HgGdpData hgGdpData = new HgGdpData();
            if (yearStr!=null){
                hgGdpData.setStatisticalYear(Integer.valueOf(yearStr));
                hgGdpData.setStatisticalTimeType(1);
            }
            if (qStr!=null){
                hgGdpData.setStatisticalQuarter(Integer.valueOf(qStr));
                hgGdpData.setStatisticalTimeType(2);
            }
            if (areaName!=null && areaName.equals("黄冈市")){
                hgGdpData.setAreaLevel(1);
            }else{
                hgGdpData.setAreaLevel(2);
            }
            if (areaName!=null){
                hgGdpData.setArea(areaName);
            }
            if (industry!=null){
                hgGdpData.setIndustryCategory(industry);
                hgGdpData.setIndustryLevel(2);
            }
            if (gdp!=null){
                hgGdpData.setGdp(Double.valueOf(gdp));
            }
            if (ranking!=null){
                hgGdpData.setRanking(Integer.valueOf(ranking));
            }
            if (growthGdp!=null){
                hgGdpData.setGrowthGdp(Double.valueOf(growthGdp));
            }
            if (growthRate!=null){
                hgGdpData.setGrowthRate(Double.valueOf(growthRate));
            }
            if (topRate!=null){
                hgGdpData.setTopRate(Double.valueOf(topRate));
            }
            if (growthRateRanking!=null){
                hgGdpData.setGrowthRateRanking(Integer.valueOf(growthRateRanking));
            }
            if (creator!=null){
                hgGdpData.setCreator(creator);
            }
            if (entryDepartment!=null){
                hgGdpData.setEntryDepartment(entryDepartment);
            }
            hgGdpDataServiceImpl.save(hgGdpData);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "导入成功");
    }

    /**
     * 导入GDP数据
     */
    @Authorization
    @RequestMapping(value = "/importGdpData",method = RequestMethod.POST)
    public Wrapper importGdpData (@RequestParam("file") MultipartFile file) {
        return hgGdpDataServiceImpl.importGdpData(file);
    }

    /**
     * 导出GDP模板
     */
    @GetMapping("/downGdpTemplate")
    public void downGdpTemplate(HttpServletResponse response) {
        HSSFWorkbook wb = new HSSFWorkbook();
        String worksheetTitle = "DP模板导入模板表";
        // 创建单元格样式
        HSSFCellStyle cellStyleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyleTitle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时不换行
        cellStyleTitle.setWrapText(false);
        // ------------------------------------------------------------------
        HSSFCellStyle cellStyle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时换行
        cellStyle.setWrapText(true);

        // ------------------------------------------------------------------
        // 设置单元格字体
        HSSFFont font = wb.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        font.setFontHeight((short) 200);
        cellStyleTitle.setFont(font);

        HSSFSheet sheet = wb.createSheet();
        // 定义第一行
        HSSFRow row1 = sheet.createRow(0);

        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计年份"));
        sheet.setColumnWidth(0, 20 * 256);;
        //第一行第2列
        cell1 = row1.createCell(1);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计月"));
        sheet.setColumnWidth(1, 20 * 256);
        //第一行第3列
        cell1 = row1.createCell(2);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计季度"));
        sheet.setColumnWidth(2, 20 * 256);
        //第一行第4列
        cell1 = row1.createCell(3);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计时间类型"));
        sheet.setColumnWidth(3, 20 * 256);
        //第一行第5列
        cell1 = row1.createCell(4);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("数据的区域"));
        sheet.setColumnWidth(4, 20 * 256);
        //第一行第6列
        cell1 = row1.createCell(5);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是市的还是区的"));
        sheet.setColumnWidth(5, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(6);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("产业名称"));
        sheet.setColumnWidth(6, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(7);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("行业名称"));
        sheet.setColumnWidth(7, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(8);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是三大产业数据还是九大行业数据还是其他, 0 是合在一起的"));
        sheet.setColumnWidth(8, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(9);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("gdp总量"));
        sheet.setColumnWidth(9, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(10);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("gpd排名"));
        sheet.setColumnWidth(10, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(11);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("增长量"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(12);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("增速"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(13);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上一级增速"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(14);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("增长率排名"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(15);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("人均gdp"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(16);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("人均gdp排名"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(17);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("贡献率"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(18);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("贡献率排名"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(19);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("拉动率"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(20);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("拉动率排名"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(21);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传的部门"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(22);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传人"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(23);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是否审核"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(24);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("审核人"));
        sheet.setColumnWidth(11, 20 * 256);


        String fileName = worksheetTitle;
        try {
            fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.reset();
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");// 指定下载的文件名
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            OutputStream output = response.getOutputStream();
            wb.write(output);
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
